import * as THREE from 'three'
import Force3 from './Force3'

class Points extends Force3 {
    constructor() {
        super()
        this.geometry = new THREE.BufferGeometry()
        this.material = null
        this.obj = null
    }

    init (param) {
        this.material = new THREE.ShaderMaterial({
            uniforms: {
                color: { value: new THREE.Color(0xffffff) },
                texture: { value: param.texture }
            },
            vertexShader: param.vs,
            fragmentShader: param.fs,
            transparent: true,
            depthWrite: false,
            blending: param.blending
        })

        this.geometry.setAttribute('position', new THREE.BufferAttribute(param.positions, 3))
        this.geometry.setAttribute('customColor', new THREE.BufferAttribute(param.colors, 3))
        this.geometry.setAttribute('vertexOpacity', new THREE.BufferAttribute(param.opacities, 1))
        this.geometry.setAttribute('size', new THREE.BufferAttribute(param.sizes, 1))

        this.obj = new THREE.Points(this.geometry, this.material)
        param.scene.add(this.obj)
    }

    updatePoints () {
        this.obj.position.copy(this.velocity)
        this.obj.geometry.attributes.position.needsUpdate = true
        this.obj.geometry.attributes.vertexOpacity.needsUpdate = true
        this.obj.geometry.attributes.size.needsUpdate = true
        this.obj.geometry.attributes.customColor.needsUpdate = true
    }
}

export default Points
